package cn.ecsd.service.impl;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import cn.ecsd.common.CommonDao;
import cn.ecsd.entity.BizLeave;
import cn.ecsd.entity.SysEmployee;
import cn.ecsd.service.LeaveService;
import cn.ecsd.util.PageSupport;

/**
 * 请假业务实现曾
 * 
 * @author 寇超强
 *
 */
public class LeaveServiceImpl implements LeaveService {
	private CommonDao commonDao;

	public CommonDao getCommonDao() {
		return commonDao;
	}

	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}

	/**
	 * 根据登陆的用户分页查询请假列表
	 */
	StringBuffer sb = new StringBuffer("");

	@Override
	public PageSupport getLeavePage(PageSupport page, BizLeave leave) {
		sb.setLength(0);
		Map<String, Object> session = ActionContext.getContext().getSession();
		// 拼接查询条件
		if ("部门经理".contentEquals((String) (session.get("employee_position")))) {
			// 如果是经理登陆，查看本部门的请假信息
			sb.append(" and l.sysEmployeeByNextDealSn.sn=" + ((SysEmployee) (session.get("employee"))).getSn());
		}
		if (!"部门经理".contentEquals((String) (session.get("employee_position")))) {
			// 如果不是经理登陆的话， 查看自己的请假信息
			sb.append(" and l.sysEmployeeByEmployeeSn.sn=" + ((SysEmployee) (session.get("employee"))).getSn());
		}
		if (leave != null) {
			if (leave.getStarttime()!= null) {
				sb.append(" and l.starttime > '" + leave.getStarttime() +"'");
			}
			if (leave.getEndtime() != null) {
				sb.append(" and l.endtime < '" + leave.getEndtime()+"'");
			}
		}

		page.setTotalCount(getTotalCount());

		String sql = "from BizLeave as l where 1=1 " + sb.toString()+" order by l.status desc "; // 获得一页数据的记录总数
		List<BizLeave> list = commonDao.queryPage(sql, page);
		page.setItems(list);
		return page;
	}

	@Override
	public int getTotalCount() {
		String getCountsql = "select count(*) from BizLeave as l where 1=1 " + sb.toString() +"order by l.id desc"; // 获得记录总数的sql
		// TODO Auto-generated method stub
		return commonDao.getCount(getCountsql);
	}
	/**
	 * 根据id获取请假详情
	 */
	@Override
	public BizLeave getLeaveById(int id) {
		// TODO Auto-generated method stub
		return (BizLeave) commonDao.get(BizLeave.class, id);
	}
	/**
	 * 实现请假审批
	 */
	@Override
	public void updatestatus(BizLeave leave) {
		// TODO Auto-generated method stub
		commonDao.update(leave);
	}
	/**
	 *  新增请假
	 */
	@Override
	public void addLeave(BizLeave leave) {
		// TODO Auto-generated method stub
		  commonDao.add(leave);
	}

}
